package com.mallcai.bigdata.ladon.utils;

import com.google.common.collect.Lists;
import com.mallcai.bigdata.ladon.dpl.entity.peekdata.PeekRule;

import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
 * @author feixy
 * @version 1.0
 * @date 2019-07-03 13:48
 */
public class CollectionUtils {

    /**
     * 集合是否为空
     * @param collection
     * @return
     */
    public static boolean isEmpty(Collection<?> collection){
        return collection == null || collection.size() == 0;
    }

    /**
     * 集合是否非空
     * @param collection
     * @return
     */
    public static boolean isNotEmpty(Collection<?> collection){
        return !isEmpty(collection);
    }

    /**
     * Map是否非空
     * @param map
     * @return
     */
    public static boolean isNotEmpty(Map<?,?> map){
        return !isEmpty(map);
    }

    /**
     * Map是否为空
     * @param
     * @return
     */
    public static boolean isEmpty(Map<?,?> map){
        return map == null || map.size() == 0;
    }

    /**
     * 将集合元素使用mapper转化为字符串,然后使用,拼接
     * @param dataList
     * @param mapper
     * @param <T>
     * @return
     */
    public static <T> String join(Collection<T> dataList, Function<T,String> mapper){
        return join(dataList, mapper, ",");
    }

    /**
     * 将集合元素使用mapper转化为字符串,然后使用指定的分隔符拼接
     * @param dataList
     * @param mapper
     * @param delimiter
     * @param <T>
     * @return
     */
    public static <T> String join(Collection<T> dataList, Function<T,String> mapper, String delimiter){
        if (isEmpty(dataList)){
            return "";
        }
        return dataList.stream().map(mapper).collect(Collectors.joining(delimiter));
    }

    public static <T,R> List<R> map(Collection<T> dataList, Function<T,R> mapper){
        return mapAndFilter(dataList, mapper, r->true);
    }

    public static <T,R> List<R> mapAndFilter(Collection<T> dataList, Function<T,R> mapper, Predicate<R> predicate){
        if(isEmpty(dataList)){
            return Lists.newArrayList();
        }
        return dataList.stream().map(mapper).filter(predicate).collect(Collectors.toList());
    }

    public static <T,R> List<R> filterAndMap(Collection<T> dataList, Function<T,R> mapper, Predicate<T> predicate){
        if(isEmpty(dataList)){
            return Lists.newArrayList();
        }
        return dataList.stream().filter(predicate).map(mapper).collect(Collectors.toList());
    }


    /**
     * 将集合转换为map
     * @param dataList
     * @param keyExtractor
     * @param valueExtractor
     * @param <T>
     * @param <K>
     * @param <V>
     * @return
     */
    public static <T,K,V> Map<K,V> toMap(Collection<T> dataList, Function<T,K> keyExtractor, Function<T,V> valueExtractor){
        return dataList.stream().collect(Collectors.toMap(keyExtractor, valueExtractor));
    }


    public static void main(String[] args) {
        List<Integer> datas = new ArrayList<>();
        datas.add(1);
        datas.add(2);
        datas.add(3);
        datas.add(4);
        System.out.println(join(datas, String::valueOf));
    }

    /**
     * 将列表使用指定的extractor转化后放到set中去重
     * @param dataList
     * @param extractor
     * @param <T>
     * @param <R>
     * @return
     */
    public static <T,R> Set<R> toSet(Collection<T> dataList, Function<T,R> extractor) {
        return dataList.stream().map(extractor).collect(Collectors.toSet());
    }

    /**
     * 判断obj1是否为集合类型
      * @param obj1
     * @return
     */
    public static boolean isCollection(Object obj1) {
        return obj1 != null && obj1 instanceof Collection;
    }

    /**
     * 获取集合中的第一个元素,如果集合为空,从supplier中复制一个
     * @param collection
     * @param supplier
     * @param <T>
     * @return
     */
    public static <T> T firstOrDefault(Collection<T> collection, Supplier<T> supplier){
        if(isNotEmpty(collection)){
            return collection.iterator().next();
        }
        return supplier.get();
    }
}
